﻿Imports Entidades
Imports Microsoft.Office.Interop.Excel
Public Class frmEntregasPorCliente
    Private cli As Cliente
    Private lista As List(Of Entrega)
    Private Sub btnVer_Click(sender As Object, e As EventArgs) Handles btnVer.Click
        cboEntregas.Items.Clear()
        If txtNombre.Text = "" Then
            MessageBox.Show("Falta nombre del cliente")
            Exit Sub
        End If
        Dim cli As Cliente = miGestion.clientePorNombre(txtNombre.Text)
        If IsNothing(cli) Then
            MessageBox.Show("No existe ese cliente")
            Exit Sub
        End If

        lista = miGestion.entregasPorCliente(cli)
        For i As Integer = 0 To lista.Count - 1
            cboEntregas.Items.Add("Entrega del dia " & lista(i).Fecha & " " & lista(i).NumeroProductos & " objetos")
        Next
        cboEntregas.Items.Add("Ver Todos los objetos")

    End Sub

    Private Sub btnObjetos_Click(sender As Object, e As EventArgs) Handles btnObjetos.Click
        DataGridView1.DataSource = ""
        If cboEntregas.Text = "" Then
            MessageBox.Show("No has seleccionado ninguna entrega")
            Exit Sub
        End If
        If cboEntregas.Text = "Ver Todos los objetos" Then
            Dim listaObj As List(Of Objeto) = miGestion.objetosDeUnCliente(miGestion.clientePorNombre(txtNombre.Text))
            DataGridView1.DataSource = listaObj
            DataGridView1.Columns("IdFamilia").Visible = False
            DataGridView1.Columns("NumeroFamilia").Visible = False
            DataGridView1.Columns("IdEntrega").Visible = False


        Else
            Dim ent As Entrega = lista(cboEntregas.SelectedIndex)
            Dim listaObj2 As List(Of Objeto) = miGestion.objetosDeUnaEntrega(ent)
            DataGridView1.DataSource = listaObj2
            DataGridView1.Columns("IdFamilia").Visible = False
            DataGridView1.Columns("NumeroFamilia").Visible = False
            DataGridView1.Columns("IdEntrega").Visible = False
            DataGridView1.Columns("NumeroSerie").DisplayIndex = 0
            DataGridView1.Columns("Descripcion").DisplayIndex = 1
            DataGridView1.Columns("Tamaño").DisplayIndex = 2
            DataGridView1.Columns("Valoracion").DisplayIndex = 3
            DataGridView1.Columns("dadoDeBaja").Visible = False







        End If


    End Sub

    Function GridAExcel(ByVal ElGrid As DataGridView) As Boolean
        'Creamos las variables
        Dim exApp As New Microsoft.Office.Interop.Excel.Application
        Dim exLibro As Microsoft.Office.Interop.Excel.Workbook
        Dim exHoja As Microsoft.Office.Interop.Excel.Worksheet
        Try
            'Añadimos el Libro al programa, y la hoja al libro
            exLibro = exApp.Workbooks.Add
            exHoja = exLibro.Worksheets.Add()
            ' ¿Cuantas columnas y cuantas filas?
            Dim NCol As Integer = ElGrid.ColumnCount
            Dim NRow As Integer = ElGrid.RowCount
            'Aqui recorremos todas las filas, y por cada fila todas las columnas y vamos escribiendo.
            For i As Integer = 1 To NCol
                exHoja.Cells.Item(1, i) = ElGrid.Columns(i - 1).Name.ToString
                'exHoja.Cells.Item(1, i).HorizontalAlignment = 3
            Next
            For Fila As Integer = 0 To NRow - 1
                For Col As Integer = 0 To NCol - 1
                    exHoja.Cells.Item(Fila + 2, Col + 1) = ElGrid.Rows(Fila).Cells(Col).Value
                Next
            Next
            'Titulo en negrita, Alineado al centro y que el tamaño de la columna se ajuste al texto
            exHoja.Rows.Item(1).Font.Bold = 1
            exHoja.Rows.Item(1).HorizontalAlignment = 3
            exHoja.Columns.AutoFit()
            'Aplicación visible
            exApp.Application.Visible = True
            exHoja = Nothing
            exLibro = Nothing
            exApp = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error al exportar a Excel")
            Return False
        End Try
        Return True
    End Function

    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        GridAExcel(DataGridView1)
    End Sub

    Private Sub frmEntregasPorCliente_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()

    End Sub
End Class